package com.swt.gap.service.impl;

import com.swt.gap.config.ServerConfig;
import com.swt.gap.dao.OntologyNodeDao;
import com.swt.gap.dao.SysIconMapper;
import com.swt.gap.dao.SysUserRoleMapper;
import com.swt.gap.model.entity.OntologyNode;
import com.swt.gap.model.entity.SysIcon;
import com.swt.gap.model.vo.CommonFileUploadVO;
import com.swt.gap.model.vo.IconVO;
import com.swt.gap.model.vo.SysIconListVO;
import com.swt.gap.model.vo.SysIconVO;
import com.swt.gap.service.CommonFileService;
import com.swt.gap.service.SysIconService;
import com.swt.gap.util.UserLoginUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysIconServiceImpl implements SysIconService {

    private final SysIconMapper sysIconMapper;

    private final OntologyNodeDao ontologyNodeDao;

    private final CommonFileService commonFileService;

    private final ServerConfig serverConfig;

    private final SysUserRoleMapper sysUserRoleMapper;

    public SysIconServiceImpl(SysIconMapper sysIconMapper, OntologyNodeDao ontologyNodeDao, CommonFileService commonFileService, ServerConfig serverConfig, SysUserRoleMapper sysUserRoleMapper) {
        this.sysIconMapper = sysIconMapper;
        this.ontologyNodeDao = ontologyNodeDao;
        this.commonFileService = commonFileService;
        this.serverConfig = serverConfig;
        this.sysUserRoleMapper = sysUserRoleMapper;
    }

    @Override
    public Integer deleteIcon(Integer iconId) {
        return sysIconMapper.updateIconByDelete(iconId);
    }

    @Override
    public List<IconVO> addIcon(MultipartFile[] files, String path) {
        List<IconVO> sysIcons = new ArrayList<>();
        Integer userId = UserLoginUtils.getUserId();
        List<CommonFileUploadVO> commonFileUploadVOS = commonFileService.uploadFile(files, path);
        commonFileUploadVOS.forEach(commonFileUploadVO -> {
            IconVO sysIcon = new IconVO();
            sysIcon.setCreatedTime(new Date());
            sysIcon.setCreatedBy(userId);
            sysIcon.setIconFile(commonFileUploadVO.getSavedFileName());
            sysIcon.setIsDeleted(false);
            sysIcon.setIconType(2);

            sysIconMapper.insertSelective(sysIcon);
            sysIcon.setViewUrl(serverConfig.parseViewUrl(commonFileUploadVO.getSavedFileName(), 3));
            sysIcons.add(sysIcon);
        });
        return sysIcons;
    }

    @Override
    public List<SysIconListVO> getAllIcons() {
        List<SysIconListVO> resultList = new ArrayList<>();
        SysIconListVO sysIconCommon = new SysIconListVO();
        sysIconCommon.setType(0);
        List<SysIconVO> sysIconVOSCommon = new ArrayList<>();

        SysIconListVO sysIconDefine = new SysIconListVO();
        sysIconDefine.setType(1);
        List<SysIconVO> sysIconVOSDefine = new ArrayList<>();

        List<SysIcon> sysIcons = sysIconMapper.selectAll();
        sysIcons.forEach(sysIcon -> {
            SysIconVO sysIconVO = new SysIconVO();
            sysIconVO.setIconFile(sysIcon.getIconFile());
            sysIconVO.setIconId(sysIcon.getIconId());
            sysIconVO.setIconType(sysIcon.getIconType());
            List<OntologyNode> record = ontologyNodeDao.selectByIconId(sysIcon.getIconId());
            if (CollectionUtils.isEmpty(record)) {
                sysIconVO.setUse(true);
            } else {
                sysIconVO.setUse(false);
            }
            sysIconVO.setViewUrl(serverConfig.parseViewUrl(sysIcon.getIconFile(), 3));
            if (sysIcon.getIconType().equals(1)) {
                sysIconVOSCommon.add(sysIconVO);
            } else if (sysIcon.getIconType().equals(2)) {
                sysIconVOSDefine.add(sysIconVO);
            }
        });
        sysIconCommon.setSysIconVOS(sysIconVOSCommon);
        sysIconDefine.setSysIconVOS(sysIconVOSDefine);
        resultList.add(sysIconCommon);
        resultList.add(sysIconDefine);
        return resultList;
    }

    @Override
    public List<IconVO> addPaintIcon(MultipartFile[] files, String path) {
        List<IconVO> sysIcons = new ArrayList<>();
        Integer userId = UserLoginUtils.getUserId();
        List<CommonFileUploadVO> commonFileUploadVOS = commonFileService.uploadFile(files, path);
        commonFileUploadVOS.forEach(commonFileUploadVO -> {
            IconVO sysIcon = new IconVO();
            sysIcon.setCreatedTime(new Date());
            sysIcon.setCreatedBy(userId);
            sysIcon.setIconFile(commonFileUploadVO.getSavedFileName());
            sysIcon.setIsDeleted(false);
            sysIcon.setIconType(3);

            sysIconMapper.insertSelective(sysIcon);
            sysIcon.setViewUrl(serverConfig.parseViewUrl(commonFileUploadVO.getSavedFileName(), 3));
            sysIcons.add(sysIcon);
        });
        return sysIcons;
    }

    @Override
    public List<SysIconVO> getPaintIcons() {
        List<SysIconVO> resultList = new ArrayList<>();
        Integer userId = UserLoginUtils.getUserId();
        List<SysIcon> sysIcons = sysIconMapper.selectAllPaints(userId);
        sysIcons.forEach(sysIcon -> {
            SysIconVO sysIconVO = new SysIconVO();
            sysIconVO.setIconFile(sysIcon.getIconFile());
            sysIconVO.setIconId(sysIcon.getIconId());
            sysIconVO.setIconType(sysIcon.getIconType());
            sysIconVO.setViewUrl(serverConfig.parseViewUrl(sysIcon.getIconFile(), 3));
            resultList.add(sysIconVO);
        });

        return resultList;
    }

    @Override
    public List<SysIconVO> listAllIcons() {
        List<SysIconVO> retValue = new ArrayList<>();
        List<SysIcon> sysIcons = sysIconMapper.selectAll();
        sysIcons.forEach(sysIcon -> {
            SysIconVO sysIconVO = SysIconVO.builder()
                    .iconFile(sysIcon.getIconFile())
                    .iconId(sysIcon.getIconId())
                    .viewUrl(serverConfig.parseViewUrl(sysIcon.getIconFile(), 3))
                    .build();
            retValue.add(sysIconVO);
        });
        return retValue;
    }

}
